package com.xiaozhen.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaozhen.blog.entity.Tag;
import com.xiaozhen.blog.entity.Type;
import com.xiaozhen.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @create 2023-05-08 10:55
 * 分类的添加删除
 */
@Controller
@RequestMapping("/admin")
public class TagAdminController {

    @Autowired
    private TagService tagService;

    /**
     * 点击导航栏的标签
     *
     * @param model
     * @return
     */
    @GetMapping("/tags")
    @PreAuthorize("hasAuthority('admin')")
    public String tags(@RequestParam(value = "page", required = false) Long page, Model model) {
        model.addAttribute("page", tagService.tagPage(page));
        return "admin/tags";
    }

    /**
     * 点击新增按钮
     *
     * @param model
     * @return
     */
    @GetMapping("/tags/input")
    @PreAuthorize("hasAuthority('admin')")
    public String addTag(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags-input";
    }


    /**
     * 新增博客标签
     * @param tag
     * @param result
     * @param attributes
     * @return
     */
    @PostMapping("/tags")
    @PreAuthorize("hasAuthority('admin')")
    public String inputTag(@Validated Tag tag, BindingResult result, RedirectAttributes attributes) {
        // 判断新增分类有没有重复的
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tag::getName, tag.getName());
        int count = tagService.count(wrapper);
        if (count != 0) {
            result.rejectValue("name", "nameError", "不能重复添加标签");
            return "admin/tags-input";
        }

        LambdaQueryWrapper<Tag> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.orderByDesc(Tag::getId);
        wrapper1.last("limit 0,1");
        Tag one = tagService.getOne(wrapper1);
        tag.setId(one.getId() + 1);
        boolean save = tagService.save(tag);
        if (!save) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/tags";
    }

    /**
     * 点击编辑按钮
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/tags/{id}/input")
    @PreAuthorize("hasAuthority('admin')")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("tag", tagService.getById(id));
        return "admin/tags-input";
    }

    /**
     * 修改博客标签
     * @param tag
     * @param result
     * @param id
     * @param attributes
     * @return
     */
    @PostMapping("/tags/{id}")
    @PreAuthorize("hasAuthority('admin')")
    public String editPost(@Validated Tag tag, BindingResult result, @PathVariable Long id, RedirectAttributes attributes) {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tag :: getName,tag.getName());
        int count = tagService.count(wrapper);
        if (count != 0){
            result.rejectValue("name", "nameError", "不能修改分类为其它分类一样的名称！");
            return "admin/tags-input";
        }
        boolean update = tagService.updateById(tag);
        if (!update) {
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/tags";
    }

    /**
     * 删除博客标签
     * @param id
     * @param attributes 返回信息
     * @return
     */
    @GetMapping("/tags/{id}/delete")
    @PreAuthorize("hasAuthority('admin')")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        tagService.removeById(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/tags";
    }
}
